package com.peter.animatedliststatedrawabledemo

import android.content.res.Resources
import android.graphics.drawable.AnimatedStateListDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import androidx.annotation.RequiresApi


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
class CheckWidgetAnimatedStateListDrawable : AnimatedStateListDrawable {
    protected var mCheckWidgetConstantState: CheckWidgetConstantState? = null

    constructor() {
        mCheckWidgetConstantState = newCheckWidgetConstantState()
    }

    protected constructor(
        res: Resources?, theme: Resources.Theme?,
        checkWidgetConstantState: CheckWidgetConstantState?
    ) {
        val drawable: Drawable
        if (checkWidgetConstantState != null) {
            drawable = if (res == null) {
                checkWidgetConstantState.mParent!!.newDrawable()
            } else if (theme == null) {
                checkWidgetConstantState.mParent!!.newDrawable(res)
            } else {
                checkWidgetConstantState.mParent!!.newDrawable(res, theme)
            }
            checkWidgetConstantState.mParent = drawable.constantState
            setConstantState((checkWidgetConstantState.mParent as DrawableContainerState?)!!)
            onStateChange(state)
            jumpToCurrentState()
            if (mCheckWidgetConstantState != null) {
                mCheckWidgetConstantState!!.grayColor = checkWidgetConstantState.grayColor
                mCheckWidgetConstantState!!.blackColor = checkWidgetConstantState.blackColor
                mCheckWidgetConstantState!!.backGroundColor =
                    checkWidgetConstantState.backGroundColor
                mCheckWidgetConstantState!!.touchAnimEnable =
                    checkWidgetConstantState.touchAnimEnable
            }
        }
    }

    override fun canApplyTheme(): Boolean {
        return true
    }

    protected fun newCheckWidgetConstantState(): CheckWidgetConstantState {
        return CheckWidgetConstantState()
    }

    override fun setConstantState(state: DrawableContainerState) {
        super.setConstantState(state)
        if (mCheckWidgetConstantState == null) {
            mCheckWidgetConstantState = newCheckWidgetConstantState()
        }
        mCheckWidgetConstantState!!.mParent = state
    }

    override fun getConstantState(): ConstantState? {
        return mCheckWidgetConstantState
    }

    class CheckWidgetConstantState : ConstantState() {
        var mParent: ConstantState? = null
        var grayColor = 0
        var blackColor = 0
        var backGroundColor = 0
        var strokeColor = 0
        var backgroundNormalAlpha = 0
        var backgroundDisableAlpha = 0
        var strokeNormalAlpha = 0
        var strokeDisableAlpha = 0
        var touchAnimEnable = false
        override fun newDrawable(): Drawable {
            return if (mParent == null) {
                null
            } else newAnimatedStateListDrawable(null, null, this)
        }

        override fun newDrawable(res: Resources?): Drawable {
            return if (mParent == null) {
                null
            } else newAnimatedStateListDrawable(res, null, this)
        }

        override fun newDrawable(res: Resources?, theme: Resources.Theme?): Drawable {
            return if (mParent == null) {
                null
            } else newAnimatedStateListDrawable(res, theme, this)
        }

        protected fun newAnimatedStateListDrawable(
            res: Resources?, theme: Resources.Theme?,
            checkWidgetConstantState: CheckWidgetConstantState?
        ): Drawable {
            return CheckWidgetAnimatedStateListDrawable(res, theme, checkWidgetConstantState)
        }

        override fun getChangingConfigurations(): Int {
            return if (mParent == null) {
                -1
            } else mParent!!.changingConfigurations
        }

        override fun canApplyTheme(): Boolean {
            return if (mParent == null) {
                false
            } else mParent!!.canApplyTheme()
        }
    }
}
